package com.shenchuangit.rydemo.web.client.base.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.shenchuangit.rydemo.web.client.base.domain.Client;
import com.shenchuangit.rydemo.web.client.base.service.IClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 客户基本信息及业务信息Controller
 *
 * @author Houxuzhong
 * @date 2021-10-09
 */
@RestController
@RequestMapping("client/base")
@Api(tags = "客户基本信息及业务信息")
public class ClientController extends BaseController {
    @Autowired
    private IClientService clientService;

    /**
     * 查询客户基本信息及业务信息列表
     */
    @PreAuthorize("@ss.hasPermi('client:base:query')")
    @GetMapping("/list")
    @ApiOperation(value = "查询客户基本信息及业务信息列表")
    public TableDataInfo list(Client client) {
        startPage();
        List<Client> list = clientService.selectClientList(client);
        return getDataTable(list);
    }

    /**
     * 导出客户基本信息及业务信息列表
     */
    @PreAuthorize("@ss.hasPermi('client:base:export')")
    @Log(title = "客户客户基本信息及业务信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value = "导出客户基本信息及业务信息列表")
    public AjaxResult export(Client client) {
        List<Client> list = clientService.selectClientList(client);
        ExcelUtil<Client> util = new ExcelUtil<Client>(Client.class);
        return util.exportExcel(list, "client");
    }

    /**
     * 获取客户基本信息及业务信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('client:base:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取客户基本信息及业务信息详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(clientService.selectClientById(id));
    }

    /**
     * 新增客户基本信息及业务信息
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('client:base:input')")
    @Log(title = "客户客户基本信息及业务信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增客户基本信息及业务信息")
    public AjaxResult add(@RequestBody Client client) {
        try{
            Client savedClient = clientService.insertClient(client);
            return AjaxResult.success(savedClient);
        }catch (CustomException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改客户基本信息及业务信息
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('client:base:input')")
    @Log(title = "客户客户基本信息及业务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改客户基本信息及业务信息")
    public AjaxResult edit(@RequestBody Client client) {
        return toAjax(clientService.updateClient(client));
    }

    /**
     * 删除客户基本信息及业务信息
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('client:base:remove')")
    @Log(title = "客户客户基本信息及业务信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除客户基本信息及业务信息")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clientService.deleteClientByIds(ids));
    }

    /**
     * 批量获取客户名称
     *
     * @param ids 客户ID
     * @return 结果
     */
    @GetMapping({"/getClientName/{clientIds}"})
    @ApiOperation(value = "批量获取客户名称")
    public AjaxResult getClientName(@ApiParam(value = "客户ID字符串") @PathVariable("clientIds") String ids) {
        String[] idArr = StringUtils.split(ids, ",");
        List<String> nameList = new ArrayList();
        Arrays.stream(idArr).forEach(id -> {
            Long userId = Long.valueOf(id);
            Client client = this.clientService.selectClientById(userId);
            String name = client.getName();
            nameList.add(name);
        });
        String nameStr = StringUtils.join(nameList, ",");
        return AjaxResult.success("批量客户姓名", nameStr);
    }
}
